use crate::prelude::*;
use actix_web::{web,HttpResponse};
use serde::{Deserialize};
use actix_session::Session;
use crate::dao::services::platform_service::ap_login;
use crate::dao::domain::user::UserDao;
#[derive(Debug,Deserialize)]
pub struct LoginForm {
    pub account:String,
    pub password:String
}

/// 默认的账号密码登录
pub async fn default_login(session:Session,login_data:web::Form<LoginForm>) -> AppResult<HttpResponse> {
    let user = ap_login(login_data.account.as_str(),login_data.password.as_str(),crate::get_db_pool!()).await?;
    session.set(UserDao::get_session_key(), user.id)?;
    Ok(HttpResponse::Ok().json(user))
}

pub fn config_login_controller(conf:&mut web::ServiceConfig) {
    conf.service(web::resource("/").route(web::post().to(default_login)));
}